package dao;


import model.Order;
import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单数据访问对象类，用于操作订单表数据
 */
public class OrderDAO {
    /**
     * 添加订单
     * @param order 要添加的订单对象
     */
    public void addOrder(Order order) {
        String sql = "INSERT INTO `order`(user_id, order_date, total_price, status) VALUES(?,?,?,?)";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, order.getUserId());
            java.sql.Date sqlDate = new java.sql.Date(order.getOrderDate().getTime());
            preparedStatement.setDate(2, sqlDate);
            preparedStatement.setDouble(3, order.getTotalPrice());
            preparedStatement.setString(4, order.getStatus());

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("订单添加成功！");
            } else {
                System.out.println("订单添加失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据订单 ID 获取订单
     * @param orderId 订单 ID
     * @return 对应的订单对象，若未找到则返回 null
     */
    public Order getOrderById(int orderId) {
        String sql = "SELECT * FROM `order` WHERE order_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, orderId);

            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                Order order = new Order();
                order.setOrderId(resultSet.getInt("order_id"));
                order.setUserId(resultSet.getInt("user_id"));
                order.setOrderDate(resultSet.getDate("order_date"));
                order.setTotalPrice(resultSet.getDouble("total_price"));
                order.setStatus(resultSet.getString("status"));

                return order;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取所有订单
     * @return 订单列表
     */
    public List<Order> getAllOrders() {
        List<Order> orderList = new ArrayList<>();
        String sql = "SELECT * FROM `order`";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            while (resultSet.next()) {
                Order order = new Order();
                order.setOrderId(resultSet.getInt("order_id"));
                order.setUserId(resultSet.getInt("user_id"));
                order.setOrderDate(resultSet.getDate("order_date"));
                order.setTotalPrice(resultSet.getDouble("total_price"));
                order.setStatus(resultSet.getString("status"));

                orderList.add(order);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return orderList;
    }

    /**
     * 根据用户 ID 获取订单列表
     * @param userId 用户 ID
     * @return 订单列表
     */
    public List<Order> getOrdersByUserId(int userId) {
        List<Order> orderList = new ArrayList<>();
        String sql = "SELECT * FROM `order` WHERE user_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, userId);

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                Order order = new Order();
                order.setOrderId(resultSet.getInt("order_id"));
                order.setUserId(resultSet.getInt("user_id"));
                order.setOrderDate(resultSet.getDate("order_date"));
                order.setTotalPrice(resultSet.getDouble("total_price"));
                order.setStatus(resultSet.getString("status"));

                orderList.add(order);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return orderList;
    }

    // 在 OrderDAO.java 中添加以下方法
    /**
     * 更新订单状态
     * @param orderId 订单 ID
     * @param status 订单状态
     */
    public void updateOrderStatus(int orderId, String status) {
        String sql = "UPDATE `order` SET status = ? WHERE order_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, status);
            preparedStatement.setInt(2, orderId);

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("订单状态更新成功！");
            } else {
                System.out.println("订单状态更新失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}